
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>socket.id Example</title>
        <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
        <script src="./lib/echarts.js"></script>

    </head>
    <body>
        <h1>websocket realtime</h1>
        <script type="text/javascript">

        </script>

        <div id="realtime" style="width: 100%;height:400px;"></div>
        <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        var realtime = echarts.init(document.getElementById('realtime'));
        var app = {};
        var base = +new Date(2014, 9, 3);
        var oneDay = 24 * 3600 * 1000;
        var date = [];

        var data = [Math.random() * 150];
        var now = new Date(base);

        function addData(shift) {
            now = [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/');
            date.push(now);
            data.push((Math.random() - 0.4) * 10 + data[data.length - 1]);

            if (shift) {
                date.shift();
                data.shift();
            }

            now = new Date(+new Date(now) + oneDay);
        }

        for (var i = 1; i < 100; i++) {
            addData();
        }

        option = {
            xAxis: {
                type: 'category',
                boundaryGap: false,
                show: false,
                data: date
            },
            yAxis: {
                boundaryGap: [0, '50%'],
                type: 'value',
                show: false
            },
            series: [{
                    name:'成交',
                    type:'line',
                    smooth:true,
                    symbol: 'none',
                    stack: 'a',
                    itemStyle: {
                        normal: {
                            color: '#47C3E9'

                        }
                    },
                    areaStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#47C3E9'
                            }, {
                                offset: 1,
                                color: '#e4f4f4'
                            }])
                        }
                    },
                    data: data
                }],
            animation: false
        };

        //app.timeTicket = setInterval(function () {
            // addData(true);
        //     realtime.setOption({
        //         xAxis: {
        //             data: date
        //         },
        //         series: [{
        //             name:'成交',
        //             data: data
        //         }]
        //     });
        // }, 2000);

        if (option && typeof option === "object") {
            realtime.setOption(option, true);
        }

        var socket = io('http://localhost:3000');
        socket.on('realtime',function(message){
            console.log(message);
            //socket.emit('realtime',{client:'client data'});
            realtime.setOption({
                xAxis: {
                    data: message.date
                },
                series: [{
                    name:'成交',
                    data: message.data
                }]
            });
        });

        </script>



        <div id="main" style="width: 600px;height:400px;"></div>
        <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {},
            legend: {
                data:['销量']
            },
            xAxis: {
                data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>
    </body>
</html>
